// common/page/camera_photo/camera_photo.js
let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      theme: app.globalData.theme,
      deviceP: options.deviceP,
      method: options.fromPage ? "faceKeyTakePhoto" : "takePhoto"
    });
    app.setNavigationBarColor();
    wx.getSystemInfo({
      success: res => {
        this.setData({
          height: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 摄像拍照
   */
  takePhoto: function(e) {
    let typeImg = e.currentTarget.dataset.type;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    const ctx = wx.createCameraContext();
    if (typeImg == "identity_img_url") {
      wx.showLoading({
        title: '正在识别中...',
        mask: true
      });
    } else {
      wx.showLoading({
        title: '正在认证头像...',
        mask: true
      });
    }
    ctx.takePhoto({
      quality: 'normal',
      success: (res) => {
        let tempFilePaths = res.tempImagePath;
        let progressUpdate = function() {};
        if (typeImg == "identity_img_url") {
          wx.showLoading({
            title: '正在识别中...',
            mask: true
          });
        } else {
          wx.showLoading({
            title: '正在认证头像...',
            mask: true
          });
        }
        app.uploadFile(tempFilePaths, progressUpdate).then(resultW => {
          if (typeImg == "identity_img_url") {
            wx.showLoading({
              title: '正在识别中...',
              mask: true
            });
            app.wxGetRequest("/app-people/read/IdCard/v1", {
              identityImg_url: app.formatImgIp(resultW)[0]
            }).then(resultData => {
              if (resultData.data.result_list[0].code == 0) {
                let peopleInfo = resultData.data.result_list[0].data;
                wx.showLoading({
                  title: '识别成功!',
                  mask: true
                });
                setTimeout(() => {
                  prevPage.setData({
                    peopleInfo: peopleInfo,
                    hidePlus: false,
                    ["obj." + "" + typeImg + ""]: [],
                    ["obj." + "" + typeImg + ""]: [app.formatImgIp(resultW)[0]],
                    ["obj." + "" + typeImg + "_show"]: [app.formatImgIp(resultW)[0]],
                  });
                  wx.navigateBack({
                    delta: getCurrentPages()
                  });
                });
              } else {
                prevPage.setData({
                  ["obj." + "" + typeImg + ""]: [],
                  peopleInfo: {},
                });
                app.alert("提示", "请拍摄清晰身份证正面图片!");
              }
            });
          } else {
            prevPage.setData({
              ["obj." + "" + typeImg + ""]: [],
              ["obj." + "" + typeImg + ""]: [resultW],
              ["obj." + "" + typeImg + "_show"]: [app.formatImgIp(resultW)[0]],
              hidePhotoPlus: false,
            });
            setTimeout(() => {
              wx.navigateBack({
                delta: getCurrentPages()
              });
            });
            // wx.showLoading({
            //   title: '正在认证头像...',
            //   mask: true
            // });

          }
        });
      }
    });

  },
  /**
   * 从人脸钥匙界面过来调此方法拍照
   */
  faceKeyTakePhoto: function(e) {
    let typeImg = e.currentTarget.dataset.type;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    wx.showLoading({
      title: '正在认证头像...',
      mask: true
    });
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'normal',
      success: (res) => {
        let tempFilePaths = res.tempImagePath;
        let progressUpdate = function() {};
        wx.showLoading({
          title: '正在认证头像...',
          mask: true
        });
        app.uploadFile(tempFilePaths, progressUpdate).then(result => {
          wx.showLoading({
            title: '正在认证头像...',
            mask: true
          });
          app.wxGetRequest("/xwzn-web-face/v3/face/photo/quality", {
            face_image_url: result
          }).then(resultData => {
            if (resultData.data.is_ok) {
              app.wxPostRequest("/api-face/v1/face/photo", {
                people_id: app.globalData.userInfo["people_id"],
                image_url: result,
              }).then(resultDt => {
                prevPage.setData({
                  facePhoto: result
                });
                wx.showLoading({
                  title: '头像合格!',
                  mask: true
                });
                setTimeout(() => {
                  prevPage.setData({
                    ["obj." + "" + typeImg + ""]: [],
                    ["obj." + "" + typeImg + ""]: [result],
                    ["obj." + "" + typeImg + "_show"]: [app.formatImgIp(result)[0]],
                    hidePhotoPlus: false,
                  });
                  wx.hideLoading();
                  wx.navigateBack({
                    delta: getCurrentPages()
                  });
                });
              });
            } else {
              prevPage.setData({
                ["obj." + "" + typeImg + ""]: []
              });
              app.alert("提示", "拍摄的人脸头像不合格!");
            }
          });
        });
      }
    });

  },
  /**
   * 取消拍照
   */
  cancelPhoto: function(e) {
    wx.navigateBack({
      delta: getCurrentPages()
    });
  },
})